package com.dnjn.happiness.emo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.domain.HappyLevel;
import com.dnjn.happiness.emo.domain.User;
import com.dnjn.happiness.emo.service.IHappyLevelService;
import com.dnjn.happiness.emo.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 分档 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-10-09
 */
@Tag(name = "e HappyLevel 分档", description = "分档")
@RestController
@RequestMapping("/happyLevel")
public class HappyLevelController extends BaseController {

    @Autowired
    protected IHappyLevelService baseService;

    @Autowired
    private IUserService userService;
    @RequiresPermissions("emo:happyLevel:query")
    @GetMapping("/getAdmin/{id}")
    public AjaxResult getAdmin(@PathVariable Long id) {
        return success(baseService.getById(id));
    }


    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @RequiresPermissions("emo:happyLevel:add")
    @PostMapping("/addAdmin")
    public AjaxResult addAdmin(@Validated @RequestBody HappyLevel domain) {
        domain.setCreateBy(SecurityUtils.getUsername());
        return toAjax(baseService.save(domain));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody HappyLevel domain) {
        domain.setCreateBy(SecurityUtils.getUsername());
        return toAjax(baseService.save(domain));
    }

    @RequiresPermissions("emo:happyLevel:edit")
    @PutMapping("/editAdmin")
    public AjaxResult editAdmin(@Validated @RequestBody HappyLevel domain) {
        return toAjax(baseService.updateById(domain));
    }


    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody HappyLevel domain) {
        return toAjax(baseService.updateById(domain));
    }

    @RequiresPermissions("emo:happyLevel:remove")
    @DeleteMapping("/delAdmin/{pkIds}")
    public AjaxResult removeAdmin(@PathVariable Long[] pkIds) {
        for (Long pkId : pkIds) {
            baseService.lambdaUpdate().eq(HappyLevel::getLevelId,pkId).set(HappyLevel::getDeleted,"1").update();
        }
        return success();
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        for (Long pkId : pkIds) {
            baseService.lambdaUpdate().eq(HappyLevel::getLevelId,pkId).set(HappyLevel::getDeleted,"1").update();
        }
        return success();
    }
    @RequiresPermissions("emo:happyLevel:query")
    @PostMapping("/pageAdmin")
    public AjaxResult pageadmin(@RequestBody HappyLevel domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<HappyLevel> list = baseService.page(page, Wrappers.query(domain).eq("deleted","0"));
        return success(list);
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestBody HappyLevel domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<HappyLevel> list = baseService.page(page, Wrappers.query(domain).eq("deleted","0"));
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, HappyLevel domain) {
        List<HappyLevel> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<HappyLevel> util = new ExcelUtil<HappyLevel>(HappyLevel.class);
        util.exportExcel(response, list, "参数数据");
    }

    @Operation(summary = "个人档案饼图数据获取")
    @GetMapping("/getLevelByUserHappinessScore/{userId}")
    public AjaxResult getLevelByUserHappinessScore(@PathVariable Long userId){
        Assert.notNull(userId,"用户Id不能为空!");
        User user = userService.lambdaQuery().eq(User::getUserId, userId).one();
        if (user == null){
            return AjaxResult.success(new HappyLevel());
        }
        Long happinessScore = user.getHappinessScore();
        HappyLevel happyLevel = new HappyLevel();
        if (!ObjectUtils.isEmpty(happinessScore)){
            happyLevel.setHappinessScore(happinessScore.intValue());
        }
        Integer maxScore = baseService.selectMaxScore();
        happyLevel.setMaxScore(maxScore);
        return AjaxResult.success(happyLevel);
    }



}